package com.poizon.utils;

import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;

/**
 * @ClassName JedisUtils
 * @Author NoCheng
 * @Date 2022/1/2 11:06
 * @Description:TODO Jedis工具
 */

public class JedisUtils {
    private static final Jedis jedis;
    static {
        //完成实例化，连接Redis服务器
        jedis=new Jedis("116.62.166.212",6379);
        jedis.auth("123456");
    }
    //新增
    public static void addString(String key,String value,long seconds){
        jedis.setex(key, seconds, value);
    }
    public static void addString(String key,String value){
        jedis.set(key, value);
    }
    public static void addHash(String key,String field,String value){
        jedis.hset(key, field, value);
    }
    public static void addHash(String key, Map<String,String> map){
        jedis.hmset(key, map);
    }
    public static void addList(String key,String value){
        jedis.lpush(key,value);
    }
    public static void addGeo(String key,double lon,double lat,String address){
        jedis.geoadd(key, lon, lat, address);
    }
    //查询
    public static String getString(String key){
        return jedis.get(key);
    }
    //校验
    public static boolean exists(String... keys){
        return jedis.exists(keys)>0;
    }
    public static boolean existsHash(String key,String field){
        return jedis.hexists(key, field);
    }
    //删除
    public static void delKey(String key){
        jedis.del(key);
    }
    public static void delHash(String key,String field){
        jedis.hdel(key,field);
    }
    //geo操作
    public static double geodist(String key,String address1,String address2){
        return jedis.geodist(key, address1, address2, GeoUnit.M);
    }
    public static List<GeoRadiusResponse> georadius(String key, double lon, double lat, double radius){
        return jedis.georadius(key,lon,lat,radius,GeoUnit.M);
    }
    //系统命令
    public static void expire(String key,long seconds){
        jedis.expire(key, seconds);
    }
    public static long ttl(String key){
        return jedis.ttl(key);
    }
}
